package world;

import objects.Player;
import media.AudioManager;
import media.Sounds;

import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Manifold;

public class WorldContactListener implements ContactListener {

	private WorldManager worldManager;
	private Player player;

	public WorldContactListener(WorldManager _worldManager, Player _player) {
		// TODO Auto-generated constructor stub
		worldManager = _worldManager;
		player = _player;
	}

	@Override
	public void beginContact(Contact contact) {
		// TODO Auto-generated method stub

		if (contact.getFixtureA().getBody().getUserData() == BodyID.PLAYER) {
			if (contact.getFixtureB().getBody().getUserData() == BodyID.COIN) {
				worldManager.readToRemove(contact.getFixtureB().getBody());
				AudioManager.playSound(Sounds.Coin);
			} else {
				if (contact.getFixtureB().getBody().getUserData() == BodyID.PROJECTILE) {
					player.setGameOver(true);
				}
			}
		} else {
			if (contact.getFixtureB().getBody().getUserData() == BodyID.PLAYER) {
				if (contact.getFixtureA().getBody().getUserData() == BodyID.COIN) {
					worldManager.readToRemove(contact.getFixtureA().getBody());
					AudioManager.playSound(Sounds.Coin);
				} else {
					if (contact.getFixtureA().getBody().getUserData() == BodyID.PROJECTILE) {
						player.setGameOver(true);
					}
				}
			}
		}

		if (contact.getFixtureA().getBody().getUserData() == BodyID.GROUND) {
			if (contact.getFixtureB().getBody().getUserData() == BodyID.PROJECTILE) {
				worldManager.readToRemove(contact.getFixtureB().getBody());
			}
		} else {
			if (contact.getFixtureB().getBody().getUserData() == BodyID.GROUND) {
				if (contact.getFixtureA().getBody().getUserData() == BodyID.PROJECTILE) {
					worldManager.readToRemove(contact.getFixtureA().getBody());
System.out.println("projectile removed");
				}
			}
		}

	}

	@Override
	public void endContact(Contact contact) {
		// TODO Auto-generated method stub

	}

	@Override
	public void preSolve(Contact contact, Manifold oldManifold) {
		// TODO Auto-generated method stub

	}

	@Override
	public void postSolve(Contact contact, ContactImpulse impulse) {
		// TODO Auto-generated method stub

	}

}
